using Microsoft.Extensions.Configuration;

namespace Lzfy_Refund_Service.Services
{
    /// <summary>
    /// 数据库配置服务 - 集中管理所有数据库连接字符串
    /// </summary>
    public class DatabaseConfigurationService : IDatabaseConfigurationService
    {
        private readonly IConfiguration _configuration;
        
        // 默认连接字符串配置 - 统一管理
        private static class DefaultConnections
        {
            public const string SqlServer = "Server=10.1.200.169,1434;Database=refunddb;User Id=sa;Password=6hospital+;TrustServerCertificate=true;";
            public const string HisOracle = "Data Source=10.1.210.6:1521/HISDB;User Id=his_bi;Password=his_bi;";
            public const string HisDGOracle = "Data Source=10.1.210.16:1521/HISDG;User Id=his_bi;Password=his_bi;";
        }

        public DatabaseConfigurationService(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        /// <summary>
        /// 获取默认SQL Server连接字符串
        /// </summary>
        public string GetDefaultConnection()
        {
            return _configuration.GetConnectionString("DefaultConnection") ?? DefaultConnections.SqlServer;
        }

        /// <summary>
        /// 获取HIS Oracle连接字符串
        /// </summary>
        public string GetHisOracleConnection()
        {
            return _configuration.GetConnectionString("HisOracleConnection") ?? DefaultConnections.HisOracle;
        }

        /// <summary>
        /// 获取HIS DG Oracle连接字符串
        /// </summary>
        public string GetHisDGOracleConnection()
        {
            return _configuration.GetConnectionString("HisDGOracleConnection") ?? DefaultConnections.HisDGOracle;
        }

        /// <summary>
        /// 获取指定名称的连接字符串
        /// </summary>
        /// <param name="connectionName">连接字符串名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public string GetConnectionString(string connectionName, string defaultValue = "")
        {
            return _configuration.GetConnectionString(connectionName) ?? defaultValue;
        }

        /// <summary>
        /// 验证连接字符串是否配置
        /// </summary>
        /// <param name="connectionName">连接字符串名称</param>
        /// <returns></returns>
        public bool IsConnectionConfigured(string connectionName)
        {
            var connectionString = _configuration.GetConnectionString(connectionName);
            return !string.IsNullOrWhiteSpace(connectionString);
        }
    }
}